<template>
    <!-- 实现通用下载 waiting 弹窗组件 -->
    <div id="downloadToServer">
        <confirmDialog ref="dialogRef" :isShowSuccessMessage="false" :count="count" :isShowLoading="false" :isShowFooter="false">
            <div style="width: 100%; text-align: center">
                <span>{{ $t('sms.waitiongTips') }}</span>
            </div>
        </confirmDialog>
    </div>
</template>

<script>
import axios from '@/api';
import { saveLogFileFun } from '@/utils/common';
export default {
    name: 'downloadToServer',
    props: {
        filePath: {
            type: String,
            default: '/files/debug_log.tar'
        },
        responseType: {
            default: 'blob'
        },
        filename: {
            type: String,
            default: 'log'
        },
        count: {
            type: Number,
            default: 5
        },
        delayTime: {
            type: Number,
            default: 2000
        },
        fileType: {
            type: String,
            default: '.txt'
        }
    },
    data() {
        return {};
    },
    components: {},
    mounted() {},
    methods: {
        async downloadFileToServer() {
            axios
                .get(this.filePath, { responseType: this.responseType })
                .then((response) => {
                    saveLogFileFun(this.filename, response, this.fileType);
                })
                .catch((error) => {
                    this.$refs.dialogRef.hide();
                    this.$message.error(this.$t('common.error'));
                    console.error(error);
                });
        },

        async delayDownLoad() {
            // 点击下载就显示loading弹窗
            this.$refs.dialogRef.show();
            this.$refs.dialogRef.changeLoadingStatus();
            // 延迟 2s 去执行下载操作
            setTimeout(() => {
                this.downloadFileToServer();
            }, this.delayTime);
        }
    }
};
</script>
<style scoped lang="less"></style>
